// 根据 key 找到相应的id值
function getUrlParams (key) {
    var str = location.search.substr(1);
    var arr = str.split('&');
    for(var i=0; i < arr.length; i++) {
        var arr1 = arr[i].split('=');
        if(arr1[0] == key) {
            return arr1[1];
        }
    }
}
var id = getUrlParams('id');
console.log(id);

$.ajax({
    type : 'get',
    url : `/posts/${id}`,
    success(res) {
        console.log(res);
        var html = template('detailTpl', res);
        $('.article').html(html)
        
    }
})

//点赞
$('.article').on('click', '#like' , function () {
    $.ajax({
        type: 'post',
        url : `/posts/fabulous/${id}`,
        success (res) {
            // console.log(res);
            alert('点赞成功，谢谢您的支持')
        }
    })
})


//评论
var state = 0; //评论的状态是否需要审核

$.ajax({
    type: 'get',
    url: 'settings',
    success (res) {
        if (res.review == false) {
            state = 0;//不需要审核
        } else {
            state = 1;//需要审核
        }
        if (res.comment == true) {
            $('.comment').show();
        }
    }
})
//评论的提交功能
$('.comment form').on('submit', function () {
    var content = $(this).find('textarea').val();
    $.ajax({
        type: 'get',
        url : '/comments',
        data: {
            content: content,
            post: id,
            state: state
        },
        success (res) {
            alert('success');
            $(this).find('textarea').val("");
            return false;
        }
    })
})